package com.king.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * Description: 日期时间工具类
 *
 * @author Xander
 * datetime: 2022-07-11 11:39
 */
public class DateTimeUtil {

    public static final String PATTERN_DATA_TIME = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_DATA = "yyyyMMdd";

    public static final String PATTERN_DATA2 = "yyyy-MM-dd";

    /**
     * 一天的毫秒数
     */
    public static final long ONE_DAY_MS = 24 * 3600 * 1000;

    /**
     * 时间戳转日期字符串
     *
     * @param timeStamp
     * @return
     */
    public static String timeStamp2DataStr(long timeStamp) {
        String dataStr = timeStamp2DataStr(timeStamp, PATTERN_DATA);
        return dataStr;
    }

    /**
     * 字符串转日期时间
     *
     * @param dateTimeStr
     * @param pattern
     * @return
     */
    public static LocalDateTime parseDateTime(String dateTimeStr, String pattern) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter);
        return localDateTime;
    }

    /**
     * 获取DateTimeFormatter
     *
     * @param pattern
     * @return
     */
    public static DateTimeFormatter getDateTimeFormatter(String pattern) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .appendPattern(pattern)
                .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                .toFormatter();
        return formatter;
    }

    /**
     * 字符串转日期
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static LocalDate parseDate(String dateStr, String pattern) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);
        LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter);
        return localDate;
    }

    /**
     * 时间戳转日期字符串
     *
     * @param timeStamp
     * @param pattern
     * @return
     */
    public static String timeStamp2DataStr(long timeStamp, String pattern) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);
        LocalDate localDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneId.systemDefault()).toLocalDate();
        String dataStr = localDate.format(dateTimeFormatter);
        return dataStr;
    }

    /**
     * 时间戳转日期时间字符串
     *
     * @param timeStamp
     * @return
     */
    public static String timeStamp2DataTimeStr(long timeStamp) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(PATTERN_DATA_TIME);
        LocalDateTime localDateTime = toLocalDateTime(timeStamp);
        String dataStr = localDateTime.format(dateTimeFormatter);
        return dataStr;
    }

    /**
     * 时间戳转日期时间字符串
     *
     * @param timeStamp
     * @param pattern
     * @return
     */
    public static String timeStamp2DataTimeStr(long timeStamp, String pattern) {
        DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern);
        LocalDateTime localDateTime = toLocalDateTime(timeStamp);
        String dataStr = localDateTime.format(dateTimeFormatter);
        return dataStr;
    }

    /**
     * Instant转LocalDateTime
     *
     * @param instant
     * @return
     */
    public static LocalDateTime toLocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * timeStamp转LocalDateTime
     *
     * @param timeStamp
     * @return
     */
    public static LocalDateTime toLocalDateTime(long timeStamp) {
        LocalDateTime localDateTime = toLocalDateTime(Instant.ofEpochMilli(timeStamp));
        return localDateTime;
    }

    /**
     * LocalDateTime 转 Instant
     *
     * @param localDateTime
     * @return
     */
    public static Instant toInstant(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant;
    }


    /**
     * 获取时间戳
     *
     * @param instant
     * @return
     */
    public static long getTimeStamp(Instant instant) {
        return instant.toEpochMilli();
    }

    /**
     * 获取今日 00:00:00 开始的 LocalDateTime
     *
     * @return
     */
    public static LocalDateTime atStartOfDay() {
        return atStartOfDay(LocalDate.now());
    }

    /**
     * LocalDate 当天对应的 00:00:00 的 LocalDateTime
     *
     * @param localDate
     * @return
     */
    public static LocalDateTime atStartOfDay(LocalDate localDate) {
        return localDate.atStartOfDay();
    }

    /**
     * timeStamp 时间戳对应日期的 00:00:00 的 时间戳
     *
     * @param timeStamp
     * @return
     */
    public static long atStartOfDay(long timeStamp) {
        LocalDate localDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneId.systemDefault()).toLocalDate();
        LocalDateTime localDateTime = atStartOfDay(localDate);
        return getTimeStamp(toInstant(localDateTime));
    }

    /**
     * timeStamp 时间戳对应日期 23:59:59 的 时间戳
     *
     * @param timeStamp
     * @return
     */
    public static long atEndOfDay(long timeStamp) {
        long endTime = atStartOfDay(timeStamp) + DateTimeUtil.ONE_DAY_MS - 1;
        return endTime;
    }

    /**
     * 获取前 days 天 00:00:00 的 时间戳
     *
     * @param days
     * @return
     */
    public static long atStartOfDayPreDays(long days) {
        LocalDateTime localDateTime = atStartOfDay().minusDays(days);
        return getTimeStamp(toInstant(localDateTime));
    }

    /**
     * 获取前 days 天 的 23:59:59 的 时间戳
     *
     * @param days
     * @return
     */
    public static long atEndOfDayPreDays(long days) {
        return atStartOfDayPreDays(days) + DateTimeUtil.ONE_DAY_MS - 1;
    }

    /**
     * 日期比较，不比较时分秒，date1是否早于date2
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isBeforeDay(Date date1, Date date2) {
        LocalDate ld1 = toLocalDateTime(date1.getTime()).toLocalDate();
        LocalDate ld2 = toLocalDateTime(date2.getTime()).toLocalDate();
        return ld1.isBefore(ld2);
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime toLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = toLocalDateTime(instant);
        return localDateTime;
    }

    /**
     * LocalDateTime转Date
     *
     * @param localDateTime
     * @return
     */
    public static Date toDate(LocalDateTime localDateTime) {
        Instant instant = toInstant(localDateTime);
        return toDate(instant);
    }

    /**
     * Date转LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate toLocalDate(Date date) {
        return toLocalDateTime(date).toLocalDate();
    }

    /**
     * LocalDate转Date
     *
     * @param localDate
     * @return
     */
    public static Date toDate(LocalDate localDate) {
        LocalDateTime localDateTime = localDate.atStartOfDay();
        return toDate(localDateTime);
    }

    /**
     * Instant 转 Date
     *
     * @param instant
     * @return
     */
    public static Date toDate(Instant instant) {
        return Date.from(instant);
    }

    /**
     * Date 转 Instant
     *
     * @param date
     * @return
     */
    public static Instant toDate(Date date) {
        return Instant.ofEpochMilli(date.getTime());
    }
    
    
    
    
    
    ////////////////////////////////////////////////////////////////
    
    public static String YYYY = "yyyy";

	public static String YYYY_MM = "yyyy-MM";

	public static String YYYY_MM_DD = "yyyy-MM-dd";

	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
			"yyyy.MM.dd HH:mm", "yyyy.MM" };

	/**
	 * 获取当前Date型日期
	 * 
	 * @return Date() 当前日期
	 */
	public static Date getNowDate() {
		return new Date();
	}

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 * 
	 * @return String
	 */
	public static String getDate() {
		return dateTimeNow(YYYY_MM_DD);
	}

	public static final String getTime() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(YYYYMMDDHHMMSS);
	}

	public static final String dateTimeNow(final String format) {
		return parseDateToStr(format, new Date());
	}

	public static final String dateTime(final Date date) {
		return parseDateToStr(YYYY_MM_DD, date);
	}

	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	public static final Date dateTime(final String format, final String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 日期路径 即年/月/日 如2018/08/08
	 */
	public static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd");
	}

	/**
	 * 日期路径 即年/月/日 如20180808
	 */
	public static final String dateTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyyMMdd");
	}

	/**
	 * 日期型字符串转化为日期 格式
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取服务器启动时间
	 */
	public static Date getServerStartDate() {
		long time = ManagementFactory.getRuntimeMXBean().getStartTime();
		return new Date(time);
	}

	/**
	 * 计算相差天数
	 */
	public static int differentDaysByMillisecond(Date date1, Date date2) {
		return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}

    
}
